/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* A frame to visualize the different OpenMath objects.
*/
public class OpenMathViewer extends JFrame {
    /**
    * The OpenMath display of the request.
    */
    private JTextArea omRequest;
    
    /**
    * The OpenMath display of the result.
    */
    private JTextArea omResult;
    
    /**
    * The WorkBook to operate with.
    */
    private WorkBook workBook;
    
    /**
    * The Constructor.
    */
    public OpenMathViewer(WorkBook wBook) {
        super("OpenMath Viewer");
        workBook = wBook;
        
        addWindowListener(
            new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    setVisible(false);
                }
            }
        );
        
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        
        final JTabbedPane tabbedPane = new JTabbedPane();
        contentPane.add(tabbedPane, BorderLayout.CENTER);
        
        omRequest = new JTextArea(20, 30);
        JScrollPane scrollPane = new JScrollPane(omRequest);
        tabbedPane.addTab("Request", scrollPane);
        
        omResult = new JTextArea(20, 30);
        omResult.setEditable(false);
        scrollPane = new JScrollPane(omResult);
        tabbedPane.addTab("Result", scrollPane);
        
        Box command = new Box(BoxLayout.X_AXIS);
        
        Insets insets = new Insets(2, 2, 2, 2);
        JButton button = new JButton("Get Selection");
        button.setMargin(insets);
        button.setToolTipText("Get the OpenMath object of the selection");
        button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    PanelJomes panelJomes = omSheet.getCurrentElement();
                    
                    if (tabbedPane.getSelectedIndex() == 0)
                        omRequest.setText(panelJomes.getJomeRequest().getOpenMathSelection());
                    else
                        omResult.setText(panelJomes.getJomeResult().getOpenMathSelection());
                }
            }
        );
        command.add(button);
        
        command.add(Box.createHorizontalGlue());
        
        button = new JButton("Set");
        button.setMargin(insets);
        button.setToolTipText("Sets the current OpenMath object");
        button.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    PanelJomes panelJomes = omSheet.getCurrentElement();
                    panelJomes.setOpenMathRequest(omRequest.getText());
                }
            }
        );
        command.add(button);
        
        contentPane.add(command, BorderLayout.SOUTH);

        pack();
    }
    
    /**
    * Sets the OpenMath object of the request.
    * @param omObj the OpenMath object.
    */
    public void setOpenMathRequest(String omObj) {
        omRequest.setText(omObj);
    }
    
    /**
    * Sets the OpenMath object of the result.
    * @param omObj the OpenMath object.
    */
    public void setOpenMathResult(String omObj) {
        omResult.setText(omObj);
    }
    
    /**
    * Clears the viewer.
    */
    public void clear() {
        omRequest.setText("");
        omResult.setText("");
    }
}
